/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.risk.service.impl;


import java.util.List;
import java.util.Map;

import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import com.koron.common.management.QrCodeAnalyserManagement;
import com.koron.common.service.QrCodeAnalyserService;
import com.koron.risk.bean.CorpBean;
import com.koron.risk.domain.GridNode;
import com.koron.risk.domain.LocalHazardSource;
import com.koron.risk.mapper.CorpManagementMapper;
import com.koron.risk.mapper.GridNodeMapper;
import com.koron.risk.mapper.LocalHazardSourceMapper;
import com.koron.risk.service.api.GridNodeService;

@Service
public class QrCodeAnalyserServiceImpl implements QrCodeAnalyserService,InitializingBean{

	@Autowired
	QrCodeAnalyserManagement management;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void afterPropertiesSet() throws Exception {
		Map map = management.getHandlers();
		map.put("LHS", this);
	}


	@Autowired
	private GridNodeService gridNodeService;

	@SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
	@Override
	public String analyser(String text) {
		SessionFactory factory = new SessionFactory();
		try{
			String[] array = text.split("_");
			if(array.length != 4){
				factory.close(false);
				return "";
			}
			String gridCode = array[1];
			String lhsCode = array[2];
			String gridName="";
			String lhsName="";
			String corpName="";
			StringBuffer ret = new StringBuffer();
			LocalHazardSourceMapper lhsMapper = factory.getMapper(LocalHazardSourceMapper.class);
			GridNodeMapper gnMapper = factory.getMapper(GridNodeMapper.class);
			CorpManagementMapper corpMapper = factory.getMapper(CorpManagementMapper.class);
			//查询危险源
			LocalHazardSource lhs = lhsMapper.queryByCode("local_hazard_source",lhsCode);
			if(lhs == null){
				factory.close(false);
				return "";
			}
			//查询网格
			GridNode gn = gnMapper.queryById(gridCode);
			if(gn == null){
				//查询部门
				MessageBean<?> deptMsg = gridNodeService.childList2(lhs.getCorpCode(),gridCode,0);
				if(deptMsg.getData() == null){
					factory.close(false);
					return "";
				}
				List<Map> deptList = (List<Map>) deptMsg.getData();
				Map deptMap = deptList.get(0);
				gridName = (String) deptMap.get("name");
			}else{
				gridName = gn.getName();
			}

			//查询公司
			CorpBean corp = corpMapper.get(lhs.getCorpCode());
			if(corp == null){
				factory.close(false);
				return "";
			}
			corpName = corp.getCorpName();
			ret.append(corpName).append(gridName).append(lhsName).append(array[3]).append("#");
			factory.close();
			return ret.toString();
		}catch (Exception e) {
			if (null!=factory)
                factory.close(false);
            return "查询异常";
		}finally {
			if (factory != null) {
				factory.close();
			}
		}
	}

}
